package icraf.carbon.gui;

import icraf.carbon.core.Info;
import icraf.carbon.gui.MenuFactory.MenuProperty;
import icraf.common.gui.MenuGenerator.IconModel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Window;
import java.util.ArrayList;
import java.util.HashSet;

import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListModel;



public class DetailPanel extends JPanel {
	private static final long serialVersionUID = -6267507992297896197L;

	public class DetailTableModel extends MatrixTableModel {
		private static final long serialVersionUID = 8968868974903218229L;
		private String header[] = { 
				MenuProperty.PROJECT_VARIABLE.getLabel(),
				MenuProperty.PROJECT_VALUE.getLabel(),
				MenuProperty.PROJECT_FORMULA.getLabel() };
		private ArrayList<Info> infoArr;
		private NumberRowHeader tableListModel = new NumberRowHeader(this);

		@Override
		public Class<?> getColumnClass(int column) {
			if (column == 1)
				return Double.class;
			return String.class;
		}

		@Override
		public int getColumnCount() {
			return header.length;
		}

		@Override
		public String getColumnName(int col) {
			return header[col];
		}


		@Override
		public int getRowCount() {
			return infoArr.size();
		}

		@Override
		public ListModel<Object> getRowHeaderModel() {
			return tableListModel;
		}


		@Override
		public Object getValueAt(int row, int column) {
			Info info = infoArr.get(row);
			switch (column) {
			case 0:
				return info.getLabel();
			case 1:
				return info.getValue()==Double.NaN?null:info.getValue();
			case 2:
				return info.getDescription();
			}
			return null;
		}

		@Override
		public boolean isCellEditable(int row, int col) {
			return false;
		}
		

		public DetailTableModel(ArrayList<Info> infoArr) {
			super();
			this.infoArr = infoArr;
			fireTableDataChanged();
			tableListModel.fireContentChange(getColumnCount());
		}

	}
	
	private JLabel labelF = new JLabel();
	private String title;
	private MatrixPanel detailPanel;
	private Color frameColor = new Color(250, 240, 200);
	
	
	public DetailPanel(String title, String label, ArrayList<Info> infoArr) {
		super();
		labelF.setText(label);
		this.title = title + " - Details";
		detailPanel = new MatrixPanel(new DetailTableModel(infoArr));
		detailPanel.getTable().setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
		detailPanel.getTable().getColumnModel().getColumn(0)
		.setPreferredWidth(250);
		detailPanel.setPreferredSize(new Dimension(400, infoArr.size()*25));
		
		labelF.setFont(labelF.getFont().deriveFont(Font.BOLD));
		this.setOpaque(true);
		this.setBackground(frameColor);
		
		this.setLayout(new BorderLayout(5,5));
		this.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		this.add(this.labelF, BorderLayout.NORTH);
		this.add(detailPanel);
	}

	public void setText(String text){
		labelF.setText(text);
	}
	

	
	public JDialog createDialog(Window owner, ModalityType modalityType) {
		JDialog dialog = new JDialog(owner, modalityType);
		dialog.getContentPane().add(this);
		dialog.setTitle(title);
		dialog.setIconImage(MenuProperty.EDIT_DETAIL.getIcon(IconModel.ICON16).getImage());
		dialog.pack();
		dialog.setVisible(true);
		return dialog;
	}

	public JLabel getLabel() {
		return labelF;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	public void setHighlightedRow(int... r) {
		HashSet<Integer> h = detailPanel.getMatrixTableModel().getHighlightRows();
		h.clear();
		for(int i: r)
			h.add(i);
	}
}